import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bmap/entity/flutter_baidu_location.dart';
import 'package:flutter_bmap/entity/flutter_baidu_location_android_option.dart';
import 'package:flutter_bmap/entity/flutter_baidu_location_ios_option.dart';
import 'package:flutter_bmap/flutter_bmap.dart';

class LocationView extends StatefulWidget {
  const LocationView({Key? key}) : super(key: key);

  @override
  State<LocationView> createState() => _LocationViewState();
}

class _LocationViewState extends State<LocationView> {
  final FlutterBmap _locationPlugin = FlutterBmap();
  StreamSubscription<Map<String, Object>>? _locationListener; //事件监听
  BaiduLocation? _baiduLocation; //经纬度信息

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("定位信息"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            _buildControlPlan(),
            const Divider(),
            _buildLocationView(),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();

    //取消监听
    if (null != _locationListener) {
      _locationListener?.cancel();
    }
  }

  /// 返回定位信息
  void _setupListener() {
    if (_locationListener != null) {
      return;
    }
    _locationListener = _locationPlugin.onResultCallback().listen((result) {
      print(result);
      setState(() {
        try {
          _baiduLocation = BaiduLocation.fromMap(result);
        } catch (e) {
          print(e);
        }
      });
    });
  }

  /// 设置android端和ios端定位参数
  void _setLocOption() {
    // android端设置定位参数
    BaiduLocationAndroidOption androidOption = BaiduLocationAndroidOption();
    androidOption.setCoorType("bd09ll"); //设置返回的位置坐标系类型
    androidOption.setIsNeedAltitude(true); // 设置是否需要返回海拔高度信息
    androidOption.setIsNeedAddres(true); // 设置是否需要返回地址信息
    androidOption.setIsNeedLocationPoiList(true); // 设置是否需要返回周边poi信息
    androidOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息
    androidOption.setIsNeedLocationDescribe(true); // 设置是否需要返回位置描述
    androidOption.setOpenGps(true); // 设置是否需要使用gps
    androidOption.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式
    androidOption.setScanspan(3000); // 设置发起定位请求时间间隔

    Map androidMap = androidOption.getMap();

    // ios 端设置定位参数
    BaiduLocationIOSOption iosOption = BaiduLocationIOSOption();
    iosOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息
    iosOption.setBMKLocationCoordinateType(
        "BMKLocationCoordinateTypeWGS84"); // 设置返回的位置坐标系类型
    iosOption.setActivityType("CLActivityTypeAutomotiveNavigation"); // 设置应用位置类型
    iosOption.setLocationTimeout(10); // 设置位置获取超时时间
    iosOption.setDesiredAccuracy("kCLLocationAccuracyBest"); // 设置预期精度参数
    iosOption.setReGeocodeTimeout(10); // 设置获取地址信息超时时间
    iosOption.setDistanceFilter(0); // 设置定位最小更新距离
    iosOption.setAllowsBackgroundLocationUpdates(true); // 是否允许后台定位
    iosOption.setPauseLocUpdateAutomatically(false); //  定位是否会被系统自动暂停
    Map iosMap = iosOption.getMap();

    _locationPlugin.prepareLoc(androidMap, iosMap);
  }

  /// 启动定位
  void _handleStartLocation() {
    _setupListener();
    _setLocOption();
    _locationPlugin.startLocation();
  }

  /// 停止定位
  void _handleStopLocation() {
    _locationPlugin.stopLocation();
    setState(() {
      _baiduLocation = null;
    });
  }

  ///////////////////////////////////////////////////
  /// 显示地理信息
  Widget _buildLocationView() {
    return _baiduLocation != null
        ? Table(
            children: [
              TableRow(children: [
                const TableCell(child: Text("经度")),
                TableCell(
                  child: Text("${_baiduLocation!.longitude}"),
                )
              ]),
              TableRow(children: [
                const TableCell(child: Text("纬度")),
                TableCell(
                  child: Text("${_baiduLocation!.latitude}"),
                )
              ]),
              TableRow(children: [
                const TableCell(child: Text("国家")),
                TableCell(
                  child: Text(_baiduLocation!.country ?? ""),
                ),
              ]),
              TableRow(children: [
                const TableCell(child: Text("省份")),
                TableCell(child: Text(_baiduLocation!.province ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("城市")),
                TableCell(child: Text(_baiduLocation!.city ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("区县")),
                TableCell(child: Text(_baiduLocation!.district ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("街道")),
                TableCell(child: Text(_baiduLocation!.street ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("地址")),
                TableCell(child: Text(_baiduLocation!.address ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("位置语义化描述")),
                TableCell(child: Text(_baiduLocation!.locationDetail ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("周边poi信息")),
                TableCell(child: Text(_baiduLocation!.poiList ?? "")),
              ]),
              TableRow(children: [
                const TableCell(child: Text("错误码")),
                TableCell(child: Text(_baiduLocation!.errorCode.toString())),
              ]),
              TableRow(children: [
                const TableCell(child: Text("定位失败描述信息")),
                TableCell(child: Text(_baiduLocation!.errorInfo ?? "")),
              ]),
            ],
          )
        : Container();
  }

  /// 控制面板
  Widget _buildControlPlan() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        MaterialButton(
          color: Colors.blue,
          textColor: Colors.white,
          onPressed: _handleStartLocation,
          child: const Text("开始定位"),
        ),
        MaterialButton(
          color: Colors.red,
          textColor: Colors.white,
          onPressed: _handleStopLocation,
          child: const Text("暂停定位"),
        )
      ],
    );
  }
}
